/**
 * @class   wosh::security::SecurityAuthenticationOS
 * @brief   

 *
 ****************************************************************************
 * @version $Id: SecurityAuthenticationOS.h 2732 2010-07-06 03:18:11Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Security_SecurityAuthenticationOS_H__
 #define __WOSH_Security_SecurityAuthenticationOS_H__

 #include <woshDefs.h>
 #include <core/Object.h>
 #include <core/SecurityToken.h>


namespace wosh {
 namespace persistence {
  namespace qt {
	class EncoderXml_Qt_Security_SecurityTokenOS;
  };
 };
 namespace security {


#define _SecurityTokenOS_VERSION		0.8399

enum SecurityTokenProviderOS_LoginType {
	LOGIN_UNKNOWN	= 0x00, // 0x00000000
	LOGIN_LOCAL		= 0x01, // 0x00000001
	LOGIN_NETWORK	= 0x02 // 0x00000010
 };

class SecurityTokenOS_Data {
	public:
		SecurityTokenOS_Data() : type(LOGIN_UNKNOWN) { }
		SecurityTokenOS_Data(const SecurityTokenOS_Data& m) {
			*this = m;
		 }
		~SecurityTokenOS_Data() { }

	public:
		SecurityTokenOS_Data& operator=(const SecurityTokenOS_Data& m) {
			if (this == &m) return *this; // Same object?
			this->os_hostname = m.os_hostname;
			this->os_name = m.os_name;
			this->type = m.type;
			return *this;
		 }

	public:
		std::string os_hostname;
		std::string os_name;
		int type;
 };

class SecurityTokenProviderOS;
class SecurityTokenAuthenticatorOS;


class SecurityTokenOS : public SecurityToken
 {
	WOSH_SERIALIZABLE(wosh::security::SecurityTokenOS)

	public:
		SecurityTokenOS()
			: SecurityToken() { }

		SecurityTokenOS( const SecurityTokenOS& m )
			: SecurityToken(m), data(m.data), os_username(m.os_username) { }

		virtual ~SecurityTokenOS() { }

	public:
		inline const std::string& getOsHostName() const	{ return this->data.os_hostname; }
		inline const std::string& getOsName() const		{ return this->data.os_name; }
		inline const std::string& getOsUsername() const	{ return this->os_username; }

		inline SecurityTokenProviderOS_LoginType getLoginType() const {
			return static_cast<SecurityTokenProviderOS_LoginType>(this->data.type);
		 }

		inline double getVersion() const				{ return _SecurityTokenOS_VERSION; }
		inline SOURCE_ACCESS getSourceAccess() const	{ return SecurityToken::ACCESS_LOCALHOST; }

	protected:
		SecurityTokenOS_Data data;
		std::string os_username;

		friend class wosh::security::SecurityTokenProviderOS;
		friend class wosh::security::SecurityTokenAuthenticatorOS;
		friend class wosh::persistence::qt::EncoderXml_Qt_Security_SecurityTokenOS;

}; // class def


class SecurityTokenProviderOS : public SecurityTokenProvider
 {
	WOSH_CLASSNAME(wosh::security::SecurityTokenProviderOS)
	WOSH_CLASS_ABSTRACT(false)
	WOSH_CLONE_CONSTRUCTOR(wosh::security::SecurityTokenProviderOS)

	public:
		SecurityTokenProviderOS();
		SecurityTokenProviderOS( const SecurityTokenProviderOS& m );
		virtual ~SecurityTokenProviderOS() { }

	public:
		inline double getVersion() const	{ return _SecurityTokenOS_VERSION; }

	public:
		virtual SecurityToken* signMessage( Message* message, const std::string& username, const std::string& os_account_name ) const;

	protected:
		virtual SecurityToken* createToken() const;
		virtual SecurityTokenOS* createToken( const std::string& os_account_name ) const;

	public:
		inline const std::string& getOsHostName() const	{ return this->data.os_hostname; }
		inline const std::string& getOsName() const		{ return this->data.os_name; }

		inline SecurityTokenProviderOS_LoginType getLoginType() const {
			return static_cast<SecurityTokenProviderOS_LoginType>(this->data.type);
		 }

		void setHostName( const std::string& host_name );
		void setOsType( SecurityTokenProviderOS_LoginType type );

	public:
		static WRESULT findLoggedUser( std::string& wosh_user, std::string* os_user = NULL );
		static WRESULT gatherOsHostName( std::string& os_hostname );
		static WRESULT gatherOsName( std::string& os_name );

	protected:
		mutable SecurityTokenOS_Data data;

}; // class def

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

class SecurityTokenAuthenticatorOS : public SecurityTokenAuthenticator
 {
	WOSH_CLASSNAME(wosh::security::SecurityTokenAuthenticatorOS)
	WOSH_CLASS_ABSTRACT(false)
//	WOSH_CLONE_CONSTRUCTOR(wosh::security::SecurityTokenAuthenticatorOS)

	public:
		SecurityTokenAuthenticatorOS();
//		SecurityTokenAuthenticatorOS( const SecurityTokenAuthenticatorOS& m );
		virtual ~SecurityTokenAuthenticatorOS() { }

	public:
		bool isSupported( const char* provider_classname, double version = 0.0 ) const;
		inline double getVersion() const	{ return _SecurityTokenOS_VERSION; }

	public:
		WRESULT validateToken( const SecurityToken* token ) const;

}; // class def


 }; // namespace security
}; // namespace wosh

#endif //__WOSH_Security_SecurityAuthenticationOS_H__
